package cn.njyd.corejava.thread.myday02;

class Account {
    private String id;
    private int balance = 1000;

    /*
    采用同步方法
     */

    public synchronized void withDraw(int money){//取款
        if (this.balance-money<0) {
            System.out.println(Thread.currentThread().getName()+"用户"+this.id+"余额不足");
            return;
        }
        this.balance-=money;
        System.out.println(Thread.currentThread().getName()+"余款成功，剩余"+this.balance);
    }
}



public class SynchronizedDemo02 {
    public static void main(String[] args) {
        Account account = new Account();
        for (int i=1;i<=20;i++){
            Thread t1 = new Thread(()->{
                account.withDraw(100);
            });
            t1.start();
        }


    }
}
